﻿using Entity = Vinno.WMS.Pack.Core.Entities.Material;
using IContractService = Vinno.WMS.Pack.Core.IWmsService;
using OutputDto = Vinno.WMS.Pack.Core.Dtos.MaterialOutputDto;

namespace Vinno.WMS.Apis.Areas.Admin.Controllers
{
    [Description("物料信息")]
    [ModuleInfo(OrderNo = 6, Position = "Wms", PositionName = "仓储模块")]
    public class MaterialController : AdminControllerBase
    {
        /// <summary>
        /// 获取 服务接口
        /// </summary>
        protected IContractService ContractService => ServiceProvider.GetService<IContractService>();

        /// <summary>
        /// 读取
        /// </summary>
        [HttpPost]
        [Description("读取")]
        [ModuleInfo]
        public PageData<OutputDto> Read(PageRequest request)
        {
            Check.NotNull(request, nameof(request));
            Expression<Func<Entity, bool>> predicate = FilterService.GetExpression<Entity>(request.FilterGroup);
            var page = ContractService.Materials.ToPage(predicate,
                request.PageCondition, m => new
                {
                    D = m,
                    IPN = m.IPN,
                    WareHouseName = m.WareHouse.Name,
                    WareHouseAreaName = m.WareHouseArea.AreaName,
                    WareHouseAreaPosName = m.WareHouseAreaPos.AreaPosName
                }).ToPageResult(data => data.Select(a =>
                    new OutputDto
                    {
                        Id = a.D.Id,
                        IPNNum = a.D.Num,
                        IPNNo = a.IPN.IpnNo,
                        IPNName = a.IPN.Name,
                        WareHouseName = a.WareHouseName,
                        WareHouseAreaName = a.WareHouseAreaName,
                        WareHouseAreaPosName = a.WareHouseAreaPosName,
                    }).ToArray());

            return page.ToPageData();
        }

    }
}